/*
 *所有关于sj_review_progress类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*sj_review_progressService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using Dapper;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using VOL.AppManager.IRepositories;
using VOL.Core.Extensions;
using VOL.Entity.DomainModels;
using VOL.Entity.ResDto;
using VOL.Entity.新文件夹;

namespace VOL.AppManager.Services
{
    public partial class sj_review_progressService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Isj_review_progressRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public sj_review_progressService(
            Isj_review_progressRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public PageGridData<sjreviewprogressdto> GetData(sjreviewprogressQuery options)
        {
            DynamicParameters sqlParams = new DynamicParameters();
            string sql = "    select  row_number() OVER () AS idx,  * from (select  * from sj_review_progress  where 1=1 order by tgl desc,citydm asc) t where 1=1  ";
            string sqlcount = "    select  count(1)  from sj_review_progress where 1=1 ";

            if (options.sq_sj != null && options.sq_sj.Count == 2)
            {
                var start_time = Convert.ToDateTime(options.sq_sj[0] + "").ToString("yyyy/MM/dd") + "";
                var end_time = Convert.ToDateTime(options.sq_sj[1] + "").ToString("yyyy/MM/dd") + "";
                //sj = sj.AddMinutes(59);
                //sj = sj.AddSeconds(59);
                sqlcount += " and a.create_time between '" + start_time + " 00:00:00' and '" + end_time + "  23:59:59'  ";
                sql += " and a.create_time between '" + start_time + " 00:00:00' and '" + end_time + " 23:59:59'  ";

            }
            if (options.sh_sj != null && options.sh_sj.Count == 2)
            {
                var start_time = Convert.ToDateTime(options.sh_sj[0] + "").ToString("yyyy/MM/dd") + "";
                var end_time = Convert.ToDateTime(options.sh_sj[1] + "").ToString("yyyy/MM/dd") + "";
                sqlcount += " and sh_time  between '" + start_time + " 00:00:00' and '" + end_time + "  23:59:59'  ";
                sql += " and sh_time  between '" + start_time + " 00:00:00' and '" + end_time + "  23:59:59'  ";
            }
            sql += "    limit @pagesize offset (@pageindex-1)*@pagesize  ";
            sqlParams.Add("pageindex", options.pageindex);
            sqlParams.Add("pagesize", options.pagesize);
            var datacount = repository.DapperContext.ExecuteScalar(sqlcount, sqlParams).GetInt();
            var data = repository.DapperContext.QueryList<sjreviewprogressdto>(sql, sqlParams).ToList();
            int totalPages = datacount / options.pagesize;
            if (datacount % options.pagesize != 0)
            {
                totalPages++;
            }
            return new PageGridData<sjreviewprogressdto> { rows = data, status = 0, total = datacount, summary = new { pagecount = totalPages } };
        }
    }
}
